package com.example.module_motocircle.helper

import android.app.Activity
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
import com.example.lib_common.dp
import com.example.lib_image.GlideApp
import com.example.module_motocircle.R
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target




/**
 *@author Wcj
 *@description
 *@date 2022/12/5 13:16
 */
class LikeGifAnimHelper2(val context: Activity) {

    private var gifDrawable: GifDrawable? = null
    private var gifImageView: ImageView? = null
    private var rootView: ViewGroup? = null

    private val animCallback = object : Animatable2Compat.AnimationCallback() {
        override fun onAnimationEnd(drawable: Drawable?) {
            super.onAnimationEnd(drawable)
            gifImageView?.visibility = View.GONE
            (gifImageView?.parent as? ViewGroup)?.removeView(gifImageView)
            mTargetView?.visibility = View.VISIBLE
            gifDrawable?.unregisterAnimationCallback(this)
        }
    }

    init {

        //   rootView = context.window.decorView.findViewById<ViewGroup>(android.R.id.content)
        if (gifImageView == null)
            gifImageView = ImageView(context)

    }

    private var mTargetView: View? = null

    fun setTargetView(view: View) {
        //先显示原来的View
        mTargetView?.visibility = View.VISIBLE
        //需要隐藏的View
        mTargetView = (view.tag as? View) ?: view
        val rootView = view.parent as ViewGroup
        mTargetView?.visibility = View.INVISIBLE
        val array = IntArray(2)
        //view.getLocationOnScreen(array)

        //获取View在父view的位置
        array[0] = view.left
        array[1] = view.top

        val imageParentView = FrameLayout(context)
        // imageParentView.setBackgroundColor(Color.BLACK)
        val lp = ConstraintLayout.LayoutParams(20.dp, 20.dp)
        lp.setMargins(array[0] - 5.dp, array[1] - 6.dp, 0, 0)
        lp.startToStart = rootView.id
        lp.topToTop = rootView.id
        imageParentView.layoutParams = lp
        rootView.addView(imageParentView)

        (gifImageView?.parent as? ViewGroup)?.removeView(gifImageView)
        gifImageView!!.visibility = View.VISIBLE
        // gifImageView!!.setBackgroundColor(Color.RED)
        val l = FrameLayout.LayoutParams(52.dp, 52.dp)
        gifImageView!!.layoutParams = l
        imageParentView.addView(gifImageView)
        //gif动画的大小，内容显示的位置都不确定，只能手动到调整位置q_q！
        l.setMargins(-(15.dp), -(10.dp), 0, 0)

        loadGif()
    }

    fun setTargetView(activity: Activity, view: View) {
        //需要隐藏的View
        mTargetView = view
        val rootView = activity.window.decorView.findViewById<ViewGroup>(android.R.id.content)
        (gifImageView?.parent as? ViewGroup)?.removeView(gifImageView)
        mTargetView?.visibility = View.INVISIBLE
        gifImageView?.visibility = View.VISIBLE
        val array = IntArray(2)

        view.getLocationOnScreen(array)

        val lp = FrameLayout.LayoutParams(51.dp, 51.dp)
        lp.setMargins(array[0] - 19.dp, array[1] - 15.dp, 0, 0)
        gifImageView!!.layoutParams = lp
        //gifImageView!!.setBackgroundColor(Color.BLACK)
        rootView.addView(gifImageView)

        loadGif()
    }


    private fun loadGif() {
        GlideApp.with(context).load(R.drawable.like)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .listener(object : RequestListener<Drawable> {

                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>,
                    isFirstResource: Boolean
                ): Boolean {
                    return true
                }

                override fun onResourceReady(
                    resource: Drawable,
                    model: Any,
                    target: Target<Drawable>?,
                    dataSource: DataSource,
                    isFirstResource: Boolean
                ): Boolean {
                    gifDrawable = resource as GifDrawable
                    gifDrawable!!.setLoopCount(1)
                    gifDrawable!!.registerAnimationCallback(animCallback)
                    return false
                }
            }).into(gifImageView!!)
    }

    fun startGif() {
        gifDrawable?.setLoopCount(1)
        gifDrawable?.start()
    }

}